package com.zhuiyun.educations.api.newCourse.service.impl;

import com.zhuiyun.educations.api.courseprice.entity.Price;
import com.zhuiyun.educations.api.courseprice.mapper.CoursePriceMapper;
import com.zhuiyun.educations.api.newCourse.entity.Course;
import com.zhuiyun.educations.api.newCourse.entity.CourseInfo;
import com.zhuiyun.educations.api.newCourse.entity.CourseUnit;
import com.zhuiyun.educations.api.newCourse.entity.DTO.CourseDTO;
import com.zhuiyun.educations.api.newCourse.entity.UnitJoint;
import com.zhuiyun.educations.api.newCourse.mapper.CourseInfoMapper;
import com.zhuiyun.educations.api.newCourse.mapper.CourseUnitMapper;
import com.zhuiyun.educations.api.newCourse.mapper.ICourseMapper;
import com.zhuiyun.educations.api.newCourse.mapper.UnitJointMapper;
import com.zhuiyun.educations.api.newCourse.service.ICourseService;
import com.zhuiyun.educations.api.rotationimage.entity.ImageFile;
import com.zhuiyun.educations.api.rotationimage.mapper.ImageFileMapper;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.BeanUtils;
import com.zhuiyun.educations.util.UploadFileUtils;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ICourseServiceImpl
 * @Description TODO 课程后端业务逻辑层实现类
 * @Author 杨友朋
 * @Date 2023/6/28 16:49
 **/
@Service
public class ICourseServiceImpl implements ICourseService {
    @Resource
    ICourseMapper iCourseMapper;

    @Resource
    CourseUnitMapper courseUnitMapper;

    @Resource
    UnitJointMapper unitJointMapper;

    @Resource
    CourseInfoMapper courseInfoMapper;

    @Resource
    UploadFileUtils uploadFileUtils;

    @Resource
    ImageFileMapper imageFileMapper;

    @Resource
    CoursePriceMapper coursePriceMapper;

    /**
     * @Author 杨友朋
     * @Description TODO 全查课程
     * @Date 2023/6/19 14:49
     * @Return PageResult
     * @param courseDTO,pageRequest
     */
    @Override
    public PageResult selectCourse(CourseDTO courseDTO, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        if(courseDTO.getPayType().equals(" ")){
            courseDTO.setPayType("");
        }
        if(courseDTO.getCourseStatus().equals(" ")){
            courseDTO.setCourseStatus("");
        }
        List<CourseDTO> list =iCourseMapper.selectCourse(courseDTO);
        return MybatisPageHelper.getPageResult(list);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 全查课程(pc端)
     * @Date 2023/6/19 14:49
     * @Return PageResult
     * @param courseDTO,pageRequest
     */
    @Override
    public PageResult selectCoursePc(CourseDTO courseDTO, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        if(courseDTO.getPayType().equals(" ")){
            courseDTO.setPayType("");
        }
        List<CourseDTO> list =iCourseMapper.selectCoursePc(courseDTO);
        return MybatisPageHelper.getPageResult(list);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 全查科目
     * @Date 2023/6/19 14:49
     * @Return PageResult
     * @param courseDTO,pageRequest
     */
    @Override
    public PageResult selectUnit(CourseDTO courseDTO, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        //查询
        List<CourseDTO> list=iCourseMapper.selectUnit(courseDTO);
        return MybatisPageHelper.getPageResult(list);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 全查详细课程
     * @Date 2023/6/19 14:49
     * @Return PageResult
     * @param courseDTO,pageRequest
     */
    @Override
    public PageResult selectJoint(CourseDTO courseDTO, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        //查询
        List<CourseDTO> list=iCourseMapper.selectJoint(courseDTO);
        return MybatisPageHelper.getPageResult(list);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加章节
     * @Date 2023/6/29 9:58
     * @Return CommonResult
     * @param courseUnit
     */
    @Override
    public CommonResult insertUnit(CourseUnit courseUnit) {
        //判断id是否为null 为 null则插入一条数据
        if(courseUnit.getId()==null){
            courseUnit.setCreateTime(new Date());
            courseUnit.setUpdateTime(new Date());
            courseUnitMapper.insertSelective(courseUnit);
            return CommonResult.ok("添加成功");
        }else {
            return CommonResult.error("添加失败");
        }

    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加详细课程
     * @Date 2023/6/29 9:59
     * @Return CommonResult
     * @param unitJoint
     */
    @Override
    public CommonResult insertJoint(UnitJoint unitJoint) {
        //判断id是否为null 为 null则插入一条数据
        if(unitJoint.getId()==null){
            unitJoint.setCreateTime(new Date());
            unitJoint.setUpdateTime(new Date());
            unitJointMapper.insertSelective(unitJoint);
            return CommonResult.ok("添加成功");
        }else {
            return CommonResult.error("添加失败");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加课程信息
     * @Date 2023/6/29 9:59
     * @Return CommonResult
     * @param imageFile,courseDTO
     */
    @Override
    @Transactional
    public CommonResult insertCourse(MultipartFile imageFile, CourseDTO courseDTO) {
        //判断id是否为空
        if(courseDTO.getId()==null){
            CourseInfo courseInfo=new CourseInfo();
            Course course=new Course();
            //id不为空 将信息存入course表
            course.setCourseName(courseDTO.getCourseTitle());
            course.setSubjectId(courseDTO.getSubjectId());
            course.setCreateTime(new Date());
            course.setUpdateTime(new Date());
            iCourseMapper.insertCourse(course);
            //获取存入course信息
            Course oneCourse = iCourseMapper.selectOneById(course.getId());
            //判断信息是否为空
            if(oneCourse!=null){
                //不为空 将将courseDTO的值赋给courseInfo
                BeanUtils.copy(courseDTO,courseInfo);
                courseInfo.setCourseId(oneCourse.getId());
                courseInfo.setCreateTime(new Date());
                courseInfo.setUpdateTime(new Date());
                //判断图片文件是否为空
                if (imageFile!=null){
                    //获取图片唯一id
                    String s=uploadFileUtils.uploadFile(imageFile);
                    //判断id是否为空
                    if(!StringUtils.isEmpty(s)) {
                        //将图片信息存入数据库
                        ImageFile image=new ImageFile();
                        String originalFilename = imageFile.getOriginalFilename();
                        String type = "";
                        int dotIndex = originalFilename.lastIndexOf(".");
                        if (dotIndex > 0 && dotIndex < originalFilename.length() - 1) {
                            type = originalFilename.substring(dotIndex + 1);
                        }
                        image.setImageName(s);
                        image.setCreateTime(new Date());
                        image.setUpdateTime(new Date());
                        image.setFileSize(imageFile.getSize());
                        image.setFileUuidName(originalFilename);
                        image.setFileType(type);
                        //持久化操作
                        imageFileMapper.insertSelective(image);
                        //将图片id赋值给courseInfo
                        courseInfo.setImageId(Integer.parseInt(s));
                    }
                }
                //将课程信息存入数据库
                courseInfoMapper.insertCourseInfo(courseInfo);
                CourseInfo c = courseInfoMapper.selectOneById(courseInfo.getId());
                //如果课程状态是收费则新增一条价钱信息
                if(c.getPayType().equals("1")) {
                    Price price = new Price();
                    price.setCourseId(c.getId());
                    price.setPrice(0.00);
                    price.setCreateTime(new Date());
                    price.setUpdateTime(new Date());
                    price.setDelFlag(0);
                    coursePriceMapper.insertSelective(price);
                }
                return CommonResult.ok();
            }
        else {
            return CommonResult.error("添加失败");
        }
    }else {
            return CommonResult.error("添加失败");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id修改课程信息
     * @Date 2023/6/29 14:06
     * @Return CommonResult
     * @param imageFile,courseDTO
     */
    @Override
    @Transactional
    public CommonResult updateCourseById(MultipartFile imageFile, CourseInfo courseInfo) {
        //判断courseInfoId是否为空
        if(courseInfo.getId()!=null){
            //判断图片id是否为空
            if(imageFile!=null) {
                //上传至服务器
                String s = uploadFileUtils.uploadFile(imageFile);
                //判断图片id是否为空
                if (!StringUtils.isEmpty(s)) {
                    ImageFile image = new ImageFile();
                    String originalFilename = imageFile.getOriginalFilename();
                    String type = "";
                    int dotIndex = originalFilename.lastIndexOf(".");
                    if (dotIndex > 0 && dotIndex < originalFilename.length() - 1) {
                        type = originalFilename.substring(dotIndex + 1);
                    }
                    image.setImageName(s);
                    image.setCreateTime(new Date());
                    image.setUpdateTime(new Date());
                    image.setFileSize(imageFile.getSize());
                    image.setFileUuidName(imageFile.getOriginalFilename());
                    image.setFileType(type);
                    //持久化操作
                    imageFileMapper.insertSelective(image);
                    courseInfo.setImageId(Integer.parseInt(s));
                    courseInfo.setUpdateTime(new Date());
                    //修改courserInfo
                    courseInfoMapper.updateCourseById(courseInfo);
                    //判断课程的付费类型
                    if (courseInfo.getPayType().equals("1")) {
                        //根据courseId单查price
                        Price p = coursePriceMapper.selectById(courseInfo.getId());
                        //判断price是否为null
                        if (p == null) {
                            //不为null新增一条price数据
                            Price price = new Price();
                            price.setCourseId(courseInfo.getId());
                            price.setPrice(0.00);
                            price.setCreateTime(new Date());
                            price.setUpdateTime(new Date());
                            price.setDelFlag(0);
                            coursePriceMapper.insertSelective(price);
                        }
                    }
                    return CommonResult.ok("修改成功");
                }else {
                    return CommonResult.error("图片id为null");
                }
            }else {
                courseInfo.setUpdateTime(new Date());
                courseInfoMapper.updateCourseById(courseInfo);
                if(courseInfo.getPayType().equals("1")){
                    Price p = coursePriceMapper.selectById(courseInfo.getId());
                    if(p==null){
                        Price price = new Price();
                        price.setCourseId(courseInfo.getId());
                        price.setPrice(0.00);
                        price.setCreateTime(new Date());
                        price.setUpdateTime(new Date());
                        price.setDelFlag(0);
                        coursePriceMapper.insertSelective(price);
                    }
                }
                return CommonResult.ok("修改成功");
            }
        }else {
            return CommonResult.error("id不能为null");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id删除课程
     * @Date 2023/6/30 9:04
     * @Return CommonResult
     * @param id
     */
    @Override
    public CommonResult delById(Integer id) {
        //判断id是否为null 不为null删除
        if(id!=null){
            courseInfoMapper.delById(id);
            return CommonResult.ok("删除成功");
        }else {
            return CommonResult.error("id为空");
        }

    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据ids批量删除
     * @Date 2023/6/30 9:05
     * @Return CommonResult
     * @param ids
     */
    @Override
    public CommonResult delByIds(List<Integer> ids) {
        //判断ids是否为null 不为null删除
        if(ids!=null){
            courseInfoMapper.delByIds(ids);
            return CommonResult.ok("删除成功");
        }else {
            return CommonResult.error("ids为空");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id修改课程状态
     * @Date 2023/6/30 9:41
     * @Return CommonResult
     * @param courseInfo
     */
    @Override
    public CommonResult upStatusById(CourseInfo courseInfo) {
        //判断id是否为null 不为null更改状态
        if(courseInfo.getId()!=null){
            //判断状态类型 0已上架 1已下架
            if(courseInfo.getCourseStatus().equals("0")){
                courseInfo.setCourseStatus("1");
                courseInfoMapper.upStatusById(courseInfo);
            }else {
                courseInfo.setCourseStatus("0");
                courseInfoMapper.upStatusById(courseInfo);
            }
            return CommonResult.ok("修改成功");
        }else {
            return CommonResult.error("id为空");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 批量上架
     * @Date 2023/6/30 10:50
     * @Return CommonResult
     * @param ids
     */
    @Override
    public CommonResult onShelf(List<Integer> ids) {
        if(ids!=null){
//            CourseInfo courseInfo=new CourseInfo();
//            for(int i=0;i<ids.size();i++){
//                courseInfo.setCourseStatus("0");
//                courseInfo.setId(ids.get(i));
//                courseInfoMapper.upStatusById(courseInfo);
//            }
            courseInfoMapper.onShelf(ids);
            return CommonResult.ok("修改成功");
        }else {
            return CommonResult.error("ids为空");
        }
    }

    /**
     * @Author 杨友朋
     * @Description TODO 批量下架
     * @Date 2023/6/30 10:51
     * @Return CommonResult
     * @param ids
     */
    @Override
    public CommonResult offShelf(List<Integer> ids) {
        if(ids!=null){
//            CourseInfo courseInfo=new CourseInfo();
//            for(int i=0;i<ids.size();i++){
//                courseInfo.setCourseStatus("1");
//                courseInfo.setId(ids.get(i));
//                courseInfoMapper.upStatusById(courseInfo);
//            }
            courseInfoMapper.offShelf(ids);
            return CommonResult.ok("修改成功");
        }else {
            return CommonResult.error("ids为空");
        }
    }

    @Override
    public CommonResult delUnitById(Integer id) {
        if (id!=null&&id!=0){
            courseUnitMapper.delUnitById(id);
            return CommonResult.ok(200);
        }
        return CommonResult.ok(500);
    }

    @Override
    public CommonResult delOneById(Integer id) {
        if (id!=null&&id!=0){
            unitJointMapper.delOneById(id);
            return CommonResult.ok(200);
        }
        return CommonResult.ok(500);
    }

    @Override
    public CommonResult upSwitch(UnitJoint unitJoint) {
        if (unitJoint.getId()!=null&&unitJoint.getId()!=0){
            unitJoint.setUpdateTime(new Date());
            unitJointMapper.upSwitch(unitJoint);
            return CommonResult.ok();
        }
        return CommonResult.error(500);
    }

    @Override
    public CommonResult upJoint(UnitJoint unitJoint) {
        if (unitJoint.getId()!=null&&unitJoint.getId()!=0){
            unitJoint.setUpdateTime(new Date());
            int i = unitJointMapper.upJoint(unitJoint);
            if(i>0){
                return CommonResult.ok();
            }
            return CommonResult.error(500);
        }
        return CommonResult.error(500);
    }

    @Override
    public List<CourseInfo> selectAll() {
        List<CourseInfo> courseInfos = courseInfoMapper.selectAll();
        return courseInfos;
    }

    @Override
    public List<CourseDTO> selectCountByTeacher() {
        return courseInfoMapper.selectCountByTeacher();
    }

    @Override
    public List<CourseDTO> selectCountBySubject() {
        return courseInfoMapper.selectCountBySubject();
    }
}
